//
//  LogOnView.m
//  uMeng
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-6-4.
//  Copyright 2011年 Mtime. All rights reserved.
//

#import "LogOnViewController.h"
#import "CONSTS.h"
#import "uMengService.h"
#import "PaseHTML.h"

@interface LogOnViewController() 

- (BOOL)checkInPut;
- (void)logOn;

@end


@implementation LogOnViewController

@synthesize delegate = _delegate;
@synthesize userInfodic = _userInfodic;
@synthesize userName = _userName;
@synthesize passWord = _passWord;

- (void)buttonPress: (id)sender
{
    UIButton *button = (UIButton*)sender;
    
    if (_buttonLogOn == button) 
    {
#warning 
        
        
        
        
        
        
    }else
    {
        if ([self checkInPut]) {
            [self performSelectorInBackground: @selector( logOn ) withObject: nil];
        }
    }
    
}

//后台登录
- (void)logOn
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSString *html= [uMengService getHTMLByUrl: [NSURL URLWithString: logOnUrl]];
    NSMutableDictionary *tempDic = [[PaseHTML getParametersFromHTML: html] retain];
    
    [tempDic setObject: _userName forKey: @"user[email]"];
    [tempDic setObject: _passWord forKey: @"user[password]"];
    TTDINFO(@"%@", tempDic);
    
    
     NSString *htmlOfLoginPage = [uMengService logOnByEmail: tempDic];
    
    TTDINFO(@"%@",  htmlOfLoginPage);
    
    // [dic release];
    [tempDic release];
    [pool release];
}

//验证输入是否正确
- (BOOL)checkInPut
{
    self.userName = [_userNameField.text stringByReplacingOccurrencesOfString: @" " withString: @""];
    self.passWord = [_passWordField.text stringByReplacingOccurrencesOfString: @" " withString: @""];
    if (_userName && _passWord && ![_userName isEqualToString: @""] && ![_passWord isEqualToString: @""]) 
    {
        return YES;
    }
    return NO;
}

//将userInfo写入plist
- (void)writeInfoToPlist
{
    int m = [_selectButton1 backgroundImageForState: UIControlStateNormal] ? 0 : 1;
    NSNumber *memorizeUserName = [NSNumber numberWithInt: m];
    int a = [_selectButton2 backgroundImageForState: UIControlStateNormal] ? 0 : 1;
    NSNumber *autoLogOn = [NSNumber numberWithInt: a];
    _userInfodic = [NSMutableDictionary dictionaryWithObjectsAndKeys: memorizeUserName, @"memorizeUserName", autoLogOn, @"autoLogOn", _userName, @"userName", _passWord, @"passWord", nil];
    [_userInfodic writeToFile: @"userInfo" atomically: YES];
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor clearColor];
    UIImageView *imageView  = [[[UIImageView alloc] initWithImage: [UIImage imageNamed: @"logOn.png"]] autorelease];
    imageView.userInteractionEnabled = YES;
    [self.view addSubview: imageView];
    
    //LogOnIfo.plist
    NSString *docs = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] ;
    NSString *subdir = [docs stringByAppendingPathComponent:@"LogOnIfo.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath: subdir]) {
        NSDictionary *dic = [[[NSMutableDictionary alloc] initWithContentsOfFile: subdir] autorelease];
        
        _memorizeUserName = [dic objectForKey: @"memorizeUserName"];
        _autoLogOn = [dic objectForKey: @"autoLogOn"];
        _userName = [dic objectForKey: @"userName"];
        _passWord = [dic objectForKey: @"passWord"];
        
    }
    
    _userNameField = [[[UITextField alloc] initWithFrame: CGRectMake(65, 18, 205, 26)] autorelease];
    _userNameField.borderStyle = UITextBorderStyleNone;
    _userNameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _userNameField.delegate = self;
    _userNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _userNameField.placeholder = @"请输入用户名";
    [self.view addSubview: _userNameField];
    if ([_memorizeUserName intValue]) {
        _userNameField.text = _userName;
    }
    
    _passWordField = [[[UITextField alloc] initWithFrame: CGRectMake(65, 56, 205, 26)] autorelease];
    _passWordField.borderStyle = UITextBorderStyleNone;
    _passWordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passWordField.clearsOnBeginEditing = YES;
    _passWordField.secureTextEntry = YES;
    
    _passWordField.delegate = self;
    _passWordField.placeholder = @"请输入密码";
    [self.view addSubview: _passWordField];
    if ([_autoLogOn intValue]) {
        _passWordField.text = _passWord;
    }
    
    _buttonLogOn = [UIButton buttonWithType: UIButtonTypeCustom];
    _buttonLogOn.frame = CGRectMake(40, 118, 75, 33);
    [_buttonLogOn setImage: [UIImage imageNamed: @"logOnbtn_cancl.png"] forState: UIControlStateHighlighted];
    [_buttonLogOn setImage: [UIImage imageNamed: @"logOnbtn_cancl_press.png"] forState: UIControlStateNormal];
    [_buttonLogOn addTarget: self action: @selector(buttonPress:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview: _buttonLogOn];
    
    _buttonCancel = [UIButton buttonWithType: UIButtonTypeCustom];
    _buttonCancel.frame = CGRectMake(170, 118, 75, 33);
    [_buttonCancel setImage: [UIImage imageNamed:  @"logOnbtn_logon.png"] forState: UIControlStateHighlighted];
    [_buttonCancel setImage: [UIImage imageNamed: @"logOnbtn_logon_press.png"] forState: UIControlStateNormal];
    [_buttonCancel addTarget: self action: @selector(buttonPress:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview: _buttonCancel];
    
    
    _selectButton1 = [UIButton buttonWithType: UIButtonTypeCustom];
    _selectButton1.frame = CGRectMake(63, 85, 28, 24);
    [self.view addSubview: _selectButton1];
    [_selectButton1 addTarget: self action: @selector(memorizePassword:) forControlEvents: UIControlEventTouchUpInside];
    [_memorizeUserName intValue]?[_selectButton1 setBackgroundImage: [UIImage imageNamed: @"login_option_select.png"] forState: UIControlStateNormal] : nil;
    
    _selectButton2 = [UIButton buttonWithType: UIButtonTypeCustom];
    _selectButton2.frame = CGRectMake(174, 85, 28, 24);
    [_selectButton2 addTarget: self action: @selector(memorizePassword:) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview: _selectButton2];
    [_autoLogOn intValue]?[_selectButton2 setBackgroundImage: [UIImage imageNamed: @"login_option_select.png"] forState: UIControlStateNormal] : nil;
}

- (void)memorizePassword:(id)sender
{
    UIButton *button = (UIButton*)sender;
    if ([button backgroundImageForState: UIControlStateNormal]) 
    {
        [button setBackgroundImage: nil forState: UIControlStateNormal];
        if (button == _selectButton1) 
        {
            [_selectButton2 setBackgroundImage: nil forState: UIControlStateNormal];
        }
    }else
    {
        [button setBackgroundImage: [UIImage imageNamed: @"login_option_select.png"] forState: UIControlStateNormal];
        
        if (button == _selectButton2) 
        {
            [_selectButton1 setBackgroundImage: [UIImage imageNamed: @"login_option_select.png"] forState: UIControlStateNormal];
        }
    }
}

- (void)dealloc
{
    TT_RELEASE_SAFELY(_userInfodic);
    TT_RELEASE_SAFELY(_userName);
    TT_RELEASE_SAFELY(_passWord);
    TT_RELEASE_SAFELY(_delegate);
    
    [super dealloc];
}

#pragma mark textFidld Delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationDuration: 0.4];
    CGRect frame = self.view.frame;
    frame.origin.y = 70;
    self.view.frame = frame;
    [UIView commitAnimations];
    return  YES;
}

@end
